﻿Imports System.Threading
Imports System.Windows.Forms.Integration
Imports Zytonic_Screenshot.PreviewControl
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Media3D
Imports System.Windows.Media
Imports System.Windows

Class ImagePreivew
    Dim WithEvents Preview As PreviewControl
    Private Declare Function GetAsyncKeyState Lib "user32" _
        (ByVal vKey As Long) As Integer
    Private Const VK_LBUTTON = &H1
    Private Const VK_RBUTTON = &H2
    Private _State As FadeState = FadeState.Inactive
    Private _OldPos As System.Drawing.Point
    Dim SkipAnim As Boolean = False
    Dim StartX, StartY, StartLeft, StartTop As Integer
    Dim IsDown As Boolean = False
    Dim IsZoomed As Boolean = False
    Dim newLeft, newtop As Double
    Dim ScaleMult As Double = 4
    Enum FadeState
        Inactive
        FadingIn
        FadingOut
    End Enum
    Enum FadeDirection
        FadeIn
        FadeOut
    End Enum
    Public Property State() As FadeState
        Get
            Return _State
        End Get
        Set(value As FadeState)
            _State = value
        End Set
    End Property
    Public Sub New(ByVal Image As String, Optional Settings As String = Nothing)
        Try
            If Settings = Nothing Then
                Preview = New PreviewControl(Settings)
                Preview.ShowInTaskbar = False
                Preview.WindowStyle = Windows.WindowStyle.None
                Preview.Opacity = 1.0
                Preview.WindowStartupLocation = Windows.WindowStartupLocation.Manual
                Preview.ResizeMode = Windows.ResizeMode.NoResize
                Dim temp As New Bitmap(Image)
                Preview.Width = 1050
                Preview.Height = temp.Height / temp.Width * 1050
                Preview.Src.ImageSource = New System.Windows.Media.Imaging.BitmapImage(New Uri(Image, UriKind.Absolute))
                Preview.Topmost = True
                Preview.Src.Stretch = Windows.Media.Stretch.Uniform
                Preview.Top = System.Windows.Forms.Cursor.Position.Y - ((temp.Height / temp.Width * 1050) / 2)
                Preview.Left = System.Windows.Forms.Cursor.Position.X - 1050 / 2
                Preview.AllowsTransparency = True
                ElementHost.EnableModelessKeyboardInterop(Preview)
                Preview.ShowActivated = False
                Preview.Show()
                temp.Dispose()
            ElseIf Settings = "Gallery" Then
                Preview = New PreviewControl(Settings)
                Preview.ShowInTaskbar = False
                Preview.WindowStyle = Windows.WindowStyle.None
                Preview.Opacity = 1.0
                Preview.WindowStartupLocation = Windows.WindowStartupLocation.Manual
                Preview.ResizeMode = Windows.ResizeMode.NoResize
                Dim temp As New Bitmap(Image)
                Preview.Width = My.Computer.Screen.Bounds.Width
                Preview.Height = My.Computer.Screen.Bounds.Height

                Dim RandomClass As New Random
                Dim X As Integer = RandomClass.Next(My.Computer.Screen.Bounds.Width / 8, My.Computer.Screen.Bounds.Width - My.Computer.Screen.Bounds.Width / 4)
                Dim y As Integer = RandomClass.Next(My.Computer.Screen.Bounds.Height / 1.35, My.Computer.Screen.Bounds.Height - My.Computer.Screen.Bounds.Height / 6)
                Preview.MyGrid.Margin = New Windows.Thickness(y - Preview.Root.Width / 2, X - Preview.Root.Height / 2, Preview.MyGrid.Margin.Right, Preview.MyGrid.Margin.Bottom)

                Preview.Src.ImageSource = New System.Windows.Media.Imaging.BitmapImage(New Uri(Image, UriKind.Absolute))
                Preview.Topmost = True
                Preview.Src.Stretch = Windows.Media.Stretch.Uniform
                Preview.Top = 0
                Preview.Left = 0
                Preview.AllowsTransparency = True
                ElementHost.EnableModelessKeyboardInterop(Preview)
                Preview.ShowActivated = False

                Preview.Show()
            ElseIf Settings = "Mouse" Then
                Preview = New PreviewControl(Settings)
                Preview.ShowInTaskbar = False
                Preview.WindowStyle = Windows.WindowStyle.None
                Preview.Opacity = 1.0
                Preview.WindowStartupLocation = Windows.WindowStartupLocation.Manual
                Preview.ResizeMode = Windows.ResizeMode.NoResize
                Dim temp As New Bitmap(Image)
                Preview.Width = My.Computer.Screen.Bounds.Width
                Preview.Height = My.Computer.Screen.Bounds.Height


                Preview.MyGrid.Margin = New Windows.Thickness(System.Windows.Forms.Cursor.Position.X - Preview.Root.Width / 2, System.Windows.Forms.Cursor.Position.Y - Preview.Root.Height / 2, Preview.MyGrid.Margin.Right, Preview.MyGrid.Margin.Bottom)

                Preview.Src.ImageSource = New System.Windows.Media.Imaging.BitmapImage(New Uri(Image, UriKind.Absolute))
                Preview.Topmost = True
                Preview.Src.Stretch = Windows.Media.Stretch.Uniform
                Preview.Top = 0
                Preview.Left = 0
                Preview.AllowsTransparency = True
                ElementHost.EnableModelessKeyboardInterop(Preview)
                Preview.ShowActivated = False

                Preview.Show()
            End If
        Catch ex As Exception
        End Try
    End Sub
    Private Sub Preview_MouseDoubleClick(sender As Object, e As Input.MouseButtonEventArgs) Handles Preview.MouseDoubleClick
        If IsZoomed = True Then
            IsZoomed = False
            ScaleRotAnimation2()
        Else
            IsZoomed = True
            ScaleRotAnimation1()
        End If
    End Sub
    Private Sub Preview_MouseRightButtonDown(sender As Object, e As Input.MouseButtonEventArgs) Handles Preview.MouseRightButtonDown
        FadeOutAnimation2()
    End Sub
    Private Sub Preview_MouseDown(sender As Object, e As Input.MouseButtonEventArgs) Handles Preview.MouseDown
        StartX = System.Windows.Forms.Cursor.Position.X
        StartLeft = Preview.MyGrid.Margin.Left
        StartY = System.Windows.Forms.Cursor.Position.Y
        StartTop = Preview.MyGrid.Margin.Top
        IsDown = True
        Dim Thread As New Thread(AddressOf MoveThread)
        Thread.Start()
    End Sub
    Private Sub Preview_MouseUp(sender As Object, e As Input.MouseButtonEventArgs) Handles Preview.MouseUp
        IsDown = False
    End Sub
    Private Sub MoveThread()
        Do While IsDown = True
            If GetAsyncKeyState(VK_LBUTTON) = False Then
                IsDown = False
            End If
            newLeft = StartLeft - (StartX - System.Windows.Forms.Cursor.Position.X) * 2
            newtop = StartTop - (StartY - System.Windows.Forms.Cursor.Position.Y) * 2
            SetPos()
            Thread.Sleep(1)
        Loop
    End Sub
    Private Sub SetPos()
        If Preview.Dispatcher.CheckAccess = False Then
            Preview.Dispatcher.Invoke(New MethodInvoker(AddressOf SetPos))
        Else
            Preview.MyGrid.Margin = New Windows.Thickness(newLeft, newtop, Preview.MyGrid.Margin.Right, Preview.MyGrid.Margin.Bottom)
        End If
    End Sub
    Private Sub ScaleRotAnimation1()
        NameScope.SetNameScope(Preview.ItemContainer, New NameScope())

        Dim transform = New ScaleTransform()
        Dim transformR = New RotateTransform
        Dim transformName = "transform"
        Dim transformNameR = "transformR"
        Preview.ItemContainer.RegisterName(transformName, transform)
        Preview.ItemContainer.RegisterName(transformNameR, transformR)


        Dim xAnimation = New DoubleAnimation
        xAnimation.From = ScaleMult / 2
        xAnimation.To = ScaleMult
        xAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rAnimation = New DoubleAnimation
        Dim yAnimation = xAnimation.Clone()

        rAnimation.From = 5
        If Preview.Settings = Nothing Then
            rAnimation.From = 0
        End If

        rAnimation.To = 0
        rAnimation.Duration = TimeSpan.FromSeconds(0.5)

        Dim storyboard = New Storyboard()
        storyboard.Children.Add(xAnimation)
        storyboard.Children.Add(yAnimation)
        storyboard.Children.Add(rAnimation)



        Dim myTransformGroup As New TransformGroup()
        myTransformGroup.Children.Add(transform)
        myTransformGroup.Children.Add(transformR)


        Preview.ItemContainer.RenderTransform = myTransformGroup

        storyboard.SetTargetProperty(xAnimation, New PropertyPath(ScaleTransform.ScaleXProperty))
        storyboard.SetTargetProperty(yAnimation, New PropertyPath(ScaleTransform.ScaleYProperty))
        storyboard.SetTargetProperty(rAnimation, New PropertyPath(RotateTransform.AngleProperty))
        'storyboard.SetTargetProperty(fAnimation, New PropertyPath())

        storyboard.SetTargetName(xAnimation, transformName)
        storyboard.SetTargetName(yAnimation, transformName)
        storyboard.SetTargetName(rAnimation, transformNameR)

        storyboard.Begin(Preview.ItemContainer)
    End Sub
    Private Sub ScaleRotAnimation2()
        NameScope.SetNameScope(Preview.ItemContainer, New NameScope())

        Dim transform = New ScaleTransform()
        Dim transformR = New RotateTransform
        Dim transformName = "transform"
        Dim transformNameR = "transformR"
        Preview.ItemContainer.RegisterName(transformName, transform)
        Preview.ItemContainer.RegisterName(transformNameR, transformR)


        Dim xAnimation = New DoubleAnimation
        xAnimation.From = ScaleMult
        xAnimation.To = ScaleMult / 2
        xAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rAnimation = New DoubleAnimation
        Dim yAnimation = xAnimation.Clone()


        rAnimation.From = 0
        rAnimation.To = 5
        If Preview.Settings = Nothing Then
            rAnimation.To = 0
        End If
        rAnimation.Duration = TimeSpan.FromSeconds(0.5)

        Dim storyboard = New Storyboard()
        storyboard.Children.Add(xAnimation)
        storyboard.Children.Add(yAnimation)
        storyboard.Children.Add(rAnimation)


        Dim myTransformGroup As New TransformGroup()
        myTransformGroup.Children.Add(transform)
        myTransformGroup.Children.Add(transformR)

        Preview.ItemContainer.RenderTransform = myTransformGroup

        storyboard.SetTargetProperty(xAnimation, New PropertyPath("(ScaleTransform.ScaleX)"))
        storyboard.SetTargetProperty(yAnimation, New PropertyPath("(ScaleTransform.ScaleY)"))
        storyboard.SetTargetProperty(rAnimation, New PropertyPath(RotateTransform.AngleProperty))

        storyboard.SetTargetName(xAnimation, transformName)
        storyboard.SetTargetName(yAnimation, transformName)
        storyboard.SetTargetName(rAnimation, transformNameR)

        storyboard.Begin(Preview.ItemContainer)
    End Sub
    Private Sub ScaleRotAnimation1_alt()
        NameScope.SetNameScope(Preview.ItemContainer, New NameScope())


        Dim angleAnimation As DoubleAnimation = New DoubleAnimation
        angleAnimation.From = -180
        angleAnimation.To = 0
        angleAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rotation As AxisAngleRotation3D = New AxisAngleRotation3D
        rotation.Axis = New Vector3D(1, 1.5, 0.5)
        Preview.Model.Transform = New RotateTransform3D(rotation)



        Dim transform = New ScaleTransform()
        Dim transformR = New RotateTransform
        Dim transformName = "transform"
        Dim transformNameR = "transformR"
        Preview.ItemContainer.RegisterName(transformName, transform)
        Preview.ItemContainer.RegisterName(transformNameR, transformR)


        Dim xAnimation = New DoubleAnimation
        xAnimation.From = 0
        xAnimation.To = ScaleMult / 2
        xAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rAnimation = New DoubleAnimation
        Dim yAnimation = xAnimation.Clone()


        rAnimation.From = 0
        rAnimation.To = 5
        If Preview.Settings = Nothing Then
            rAnimation.To = 0
        End If
        rAnimation.Duration = TimeSpan.FromSeconds(0.5)

        Dim storyboard = New Storyboard()
        storyboard.Children.Add(xAnimation)
        storyboard.Children.Add(yAnimation)
        storyboard.Children.Add(rAnimation)


        Dim myTransformGroup As New TransformGroup()
        myTransformGroup.Children.Add(transform)
        myTransformGroup.Children.Add(transformR)

        Preview.ItemContainer.RenderTransform = myTransformGroup

        storyboard.SetTargetProperty(xAnimation, New PropertyPath("(ScaleTransform.ScaleX)"))
        storyboard.SetTargetProperty(yAnimation, New PropertyPath("(ScaleTransform.ScaleY)"))
        storyboard.SetTargetProperty(rAnimation, New PropertyPath(RotateTransform.AngleProperty))

        storyboard.SetTargetName(xAnimation, transformName)
        storyboard.SetTargetName(yAnimation, transformName)
        storyboard.SetTargetName(rAnimation, transformNameR)

        rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, angleAnimation)
        storyboard.Begin(Preview.ItemContainer)
    End Sub
    Private Sub ScaleRotAnimation2_alt()
        NameScope.SetNameScope(Preview.ItemContainer, New NameScope())

        Dim angleAnimation As DoubleAnimation = New DoubleAnimation
        angleAnimation.From = 0
        angleAnimation.To = 180
        angleAnimation.Duration = TimeSpan.FromSeconds(0.75)
        Dim rotation As AxisAngleRotation3D = New AxisAngleRotation3D
        rotation.Axis = New Vector3D(1, 1.5, 0.5)
        Preview.Model.Transform = New RotateTransform3D(rotation)


        Dim transform = New ScaleTransform()
        Dim transformR = New RotateTransform
        Dim transformName = "transform"
        Dim transformNameR = "transformR"
        Preview.ItemContainer.RegisterName(transformName, transform)
        Preview.ItemContainer.RegisterName(transformNameR, transformR)


        Dim xAnimation = New DoubleAnimation
        xAnimation.From = ScaleMult
        xAnimation.To = 0
        xAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rAnimation = New DoubleAnimation
        Dim yAnimation = xAnimation.Clone()


        rAnimation.From = 0
        rAnimation.To = 5
        If Preview.Settings = Nothing Then
            rAnimation.To = 0
        End If
        rAnimation.Duration = TimeSpan.FromSeconds(0.5)

        Dim storyboard = New Storyboard()
        storyboard.Children.Add(xAnimation)
        storyboard.Children.Add(yAnimation)
        storyboard.Children.Add(rAnimation)


        Dim myTransformGroup As New TransformGroup()
        myTransformGroup.Children.Add(transform)
        myTransformGroup.Children.Add(transformR)

        Preview.ItemContainer.RenderTransform = myTransformGroup

        storyboard.SetTargetProperty(xAnimation, New PropertyPath("(ScaleTransform.ScaleX)"))
        storyboard.SetTargetProperty(yAnimation, New PropertyPath("(ScaleTransform.ScaleY)"))
        storyboard.SetTargetProperty(rAnimation, New PropertyPath(RotateTransform.AngleProperty))

        storyboard.SetTargetName(xAnimation, transformName)
        storyboard.SetTargetName(yAnimation, transformName)
        storyboard.SetTargetName(rAnimation, transformNameR)

        rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, angleAnimation)
        storyboard.Begin(Preview.ItemContainer)
    End Sub
    Private Sub ScaleRotAnimation2_Alt2()
        NameScope.SetNameScope(Preview.ItemContainer, New NameScope())


        Dim angleAnimation As DoubleAnimation = New DoubleAnimation
        angleAnimation.From = 0
        angleAnimation.To = 180
        angleAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rotation As AxisAngleRotation3D = New AxisAngleRotation3D
        rotation.Axis = New Vector3D(1, 1.5, 0.5)
        Preview.Model.Transform = New RotateTransform3D(rotation)



        Dim transform = New ScaleTransform()
        Dim transformR = New RotateTransform
        Dim transformName = "transform"
        Dim transformNameR = "transformR"
        Preview.ItemContainer.RegisterName(transformName, transform)
        Preview.ItemContainer.RegisterName(transformNameR, transformR)


        Dim xAnimation = New DoubleAnimation
        xAnimation.From = ScaleMult / 2
        xAnimation.To = 0
        xAnimation.Duration = TimeSpan.FromSeconds(0.5)
        Dim rAnimation = New DoubleAnimation
        Dim yAnimation = xAnimation.Clone()


        rAnimation.From = 0
        rAnimation.To = 5
        If Preview.Settings = Nothing Then
            rAnimation.To = 0
        End If
        rAnimation.Duration = TimeSpan.FromSeconds(0.5)

        Dim storyboard = New Storyboard()
        storyboard.Children.Add(xAnimation)
        storyboard.Children.Add(yAnimation)
        storyboard.Children.Add(rAnimation)


        Dim myTransformGroup As New TransformGroup()
        myTransformGroup.Children.Add(transform)
        myTransformGroup.Children.Add(transformR)

        Preview.ItemContainer.RenderTransform = myTransformGroup

        storyboard.SetTargetProperty(xAnimation, New PropertyPath("(ScaleTransform.ScaleX)"))
        storyboard.SetTargetProperty(yAnimation, New PropertyPath("(ScaleTransform.ScaleY)"))
        storyboard.SetTargetProperty(rAnimation, New PropertyPath(RotateTransform.AngleProperty))

        storyboard.SetTargetName(xAnimation, transformName)
        storyboard.SetTargetName(yAnimation, transformName)
        storyboard.SetTargetName(rAnimation, transformNameR)

        rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, angleAnimation)
        storyboard.Begin(Preview.ItemContainer)
    End Sub
    Private Sub GalleryAnimation()
        NameScope.SetNameScope(Preview, New NameScope())

        Dim buttonMatrixTransform As New MatrixTransform()
        Preview.RenderTransform = buttonMatrixTransform

        Preview.RegisterName("ButtonMatrixTransform", buttonMatrixTransform)


        ' Create the animation path.
        Dim animationPath As New PathGeometry()
        Dim pFigure As New PathFigure()
        pFigure.StartPoint = New Point(600, 0)


        Dim pBezierSegment As New PolyBezierSegment


        pBezierSegment.Points.Add(New Point(0, 0))

        pBezierSegment.Points.Add(New Point(30, 40))

        pBezierSegment.Points.Add(New Point(100, 50))

        pBezierSegment.IsSmoothJoin = True


        pFigure.Segments.Add(pBezierSegment)

        animationPath.Figures.Add(pFigure)

        ' Freeze the PathGeometry for performance benefits.
        animationPath.Freeze()

        ' Create a MatrixAnimationUsingPath to move the
        ' button along the path by animating
        ' its MatrixTransform.
        Dim matrixAnimation As New MatrixAnimationUsingPath()
        matrixAnimation.PathGeometry = animationPath
        matrixAnimation.Duration = TimeSpan.FromSeconds(4)

        matrixAnimation.AutoReverse = False
        matrixAnimation.IsAdditive = False


        'matrixAnimation.RepeatBehavior = RepeatBehavior.Forever

        matrixAnimation.DoesRotateWithTangent = True

        ' Set the animation to target the Matrix property
        ' of the MatrixTransform named "ButtonMatrixTransform".
        Storyboard.SetTargetName(matrixAnimation, "ButtonMatrixTransform")
        Storyboard.SetTargetProperty(matrixAnimation, New PropertyPath(MatrixTransform.MatrixProperty))

        ' Create a Storyboard to contain and apply the animation.
        Dim pathAnimationStoryboard As New Storyboard()
        pathAnimationStoryboard.Children.Add(matrixAnimation)

        ' Start the storyboard when the button is loaded.
        pathAnimationStoryboard.Begin(Preview)
    End Sub
    Public Sub Dispose()
        ClosePreview()
    End Sub
    Private Sub Preview_Load(sender As Object, e As EventArgs) Handles Preview.Loaded
        If Preview.Settings = Nothing Then
            ScaleRotAnimation1_alt()
            _State = FadeState.FadingIn
            Dim Thread1 As New Thread(AddressOf MouseCheck)
            Thread1.Start()

            Dim Thread2 As New Thread(AddressOf StateThread)
            Thread2.Start()
        ElseIf Preview.Settings = "Gallery" Then
            ScaleRotAnimation1_alt()
        ElseIf Preview.Settings = "Mouse" Then
            ScaleRotAnimation1_alt()
        End If
    End Sub
    Private Sub MouseCheck()
        _OldPos = System.Windows.Forms.Cursor.Position
        Do While _OldPos = System.Windows.Forms.Cursor.Position
            Thread.Sleep(10)
        Loop
        If SkipAnim = False Then
            FadeOutAnimation()
        End If
    End Sub
    Private Sub CloseThread()
        Dim EndSpan As DateTime = DateTime.Now.AddMilliseconds(500)

        Do Until DateTime.Now.TimeOfDay > EndSpan.TimeOfDay
            Thread.Sleep(10)
        Loop
        ClosePreview()
    End Sub
    Private Sub LoadThread2()
        Dim EndSpan As DateTime = DateTime.Now.AddMilliseconds(100)

        Do Until DateTime.Now.TimeOfDay > EndSpan.TimeOfDay
            Thread.Sleep(10)
        Loop
        FadeInAnimation2()
    End Sub
    Private Sub StateThread()
        Dim EndSpan As DateTime = DateTime.Now.AddMilliseconds(500)

        Do Until DateTime.Now.TimeOfDay > EndSpan.TimeOfDay
            Thread.Sleep(10)
        Loop
        _State = FadeState.Inactive
    End Sub
    Private Sub StateThread2()
        Dim EndSpan As DateTime = DateTime.Now.AddMilliseconds(3000)

        Do Until DateTime.Now.TimeOfDay > EndSpan.TimeOfDay
            Thread.Sleep(10)
        Loop
        _State = FadeState.Inactive
        FadeOutAnimation2()
    End Sub
    Private Sub FadeInAnimation2()
        If Preview.Dispatcher.CheckAccess = False Then
            Preview.Dispatcher.Invoke(New MethodInvoker(AddressOf FadeInAnimation2))
        Else
            _State = FadeState.FadingIn
            Dim angleAnimation As DoubleAnimation = New DoubleAnimation
            angleAnimation.From = -120
            angleAnimation.To = 0
            angleAnimation.Duration = TimeSpan.FromSeconds(0.5)
            Dim rotation As AxisAngleRotation3D = New AxisAngleRotation3D
            rotation.Axis = New Vector3D(1, 1.5, 0.5)
            Preview.Model.Transform = New RotateTransform3D(rotation)
            rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, angleAnimation)
            Preview.Opacity = 1.0

            Dim Thread2 As New Thread(AddressOf StateThread2)
            Thread2.Start()
        End If
    End Sub
    Private Sub FadeInAnimation()
        If Preview.Dispatcher.CheckAccess = False Then
            Preview.Dispatcher.Invoke(New MethodInvoker(AddressOf FadeInAnimation))
        Else
            _State = FadeState.FadingIn
            Dim angleAnimation As DoubleAnimation = New DoubleAnimation
            angleAnimation.From = -120
            angleAnimation.To = 0
            angleAnimation.Duration = TimeSpan.FromSeconds(0.5)
            Dim rotation As AxisAngleRotation3D = New AxisAngleRotation3D
            rotation.Axis = New Vector3D(1, 1.5, 0.5)
            Preview.Model.Transform = New RotateTransform3D(rotation)
            rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, angleAnimation)
            Preview.Opacity = 1.0

            Dim Thread1 As New Thread(AddressOf MouseCheck)
            Thread1.Start()

            Dim Thread2 As New Thread(AddressOf StateThread)
            Thread2.Start()
        End If
    End Sub
    Private Sub FadeOutAnimation()
        If Preview.Dispatcher.CheckAccess = False Then
            Do Until _State = FadeState.Inactive
                Thread.Sleep(25)
            Loop
            Preview.Dispatcher.Invoke(New MethodInvoker(AddressOf FadeOutAnimation))
        Else
            If IsZoomed = True Then
                ScaleRotAnimation2_alt()
            Else
                ScaleRotAnimation2_Alt2()
            End If
            Dim thread As New Thread(AddressOf CloseThread)
            thread.Start()
        End If
    End Sub
    Private Sub FadeOutAnimation2()
        If Preview.Dispatcher.CheckAccess = False Then
            Do Until _State = FadeState.Inactive
                Thread.Sleep(25)
            Loop
            Preview.Dispatcher.Invoke(New MethodInvoker(AddressOf FadeOutAnimation2))
        Else
            If IsZoomed = True Then
                ScaleRotAnimation2_alt()
            Else
                ScaleRotAnimation2_Alt2()
            End If
            Dim thread As New Thread(AddressOf CloseThread)
            thread.Start()
        End If
    End Sub
    Private Sub ClosePreview()
        If Preview.Dispatcher.CheckAccess = False Then
            Preview.Dispatcher.Invoke(New MethodInvoker(AddressOf ClosePreview))
        Else
            Preview.Close()
        End If
    End Sub
End Class